package general;

import org.apache.log4j.Logger;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class GlobalProperties {

    private static Logger log = Logger.getLogger(GlobalProperties.class);

    public static final String FOLDER_MONIT = "folderMonit";
    public static final String USERNAME = "username";
    public static final String PASSWORD = "password";
    public static final String TIMER = "timer";
    public static final String SESSION_KEY = "session_key";
    public static final String USER = "user";
    public static final String PROFILES = "profiles";
    public static final String DEFAULT_ATTRIBUTE = "brak";

    public static boolean isLogged = false;
    public static InputStream inputStream;
    public static Properties properties;
    public static File globalPropertiesFile;
    public static File settingsDirectory;

    public GlobalProperties() {
    }

    static {
	properties = new Properties();
	inputStream = GlobalProperties.class
		.getResourceAsStream("/resources/global.properties");
	try {
	    getSettingsDirectory();
	    File toWrite = new File(settingsDirectory, "\\global.properties");
	    if (!toWrite.exists())
		writeToFile(inputStream, toWrite);
	    globalPropertiesFile = toWrite;
	    properties = FileUtils.loadProperties(globalPropertiesFile);
	
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error(e.getMessage());
	}
    }

    public static void getSettingsDirectory() {
	String userHome = System.getProperty("user.home");
	if (userHome == null) {
	    throw new IllegalStateException("user.home==null");
	}

	File home = new File(userHome);
	settingsDirectory = new File(home, "dyplom");
	if (!settingsDirectory.exists()) {
	    if (!settingsDirectory.mkdir()) {
		throw new IllegalStateException(settingsDirectory.toString());
	    }
	}
    }

    public static void hide(File src) throws InterruptedException, IOException {
	Process p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
	p.waitFor();
    }

    public static void unhide(File src) throws InterruptedException,
	    IOException {
	Process p = Runtime.getRuntime().exec("attrib -h " + src.getPath());
	p.waitFor();
    }

    public static void loadProperties(File fileProperties) {
	InputStream is;
	try {
	    is = new FileInputStream(fileProperties);
	    properties.load(is);
	    System.out.println("asd");
	} catch (FileNotFoundException e) {
	    log.error(e.getMessage());
	} catch (IOException e) {
	    log.error(e.getMessage());
	}
    }

    public static String getFolderMonit() {
	return properties.get(FOLDER_MONIT).toString();
    }

    public static String getUsername() {
	return properties.get(USERNAME).toString();
    }

    public static String getPassword() {
	return properties.get(PASSWORD).toString();
    }

    public static long getTimer() {
	return Long.parseLong(properties.get(TIMER).toString());
    }

    public static String getSessionKey() {
	return properties.get(SESSION_KEY).toString();
    }

    public static String getProfiles() {
	return properties.get(PROFILES).toString();
    }

    public static String getProfileId(String profileName) {
	for (String full : getProfiles().split("\\*")) {
	    int id = full.indexOf("?");
	    String name = full.substring(0, id);
	    if (name.equals(profileName))
		return full.substring(id + 1, full.length());
	}

	return null;
    }

    public static void writeToFile(InputStream is, File file) {
	try {
	    DataOutputStream out = new DataOutputStream(
		    new BufferedOutputStream(new FileOutputStream(file)));
	    int c;
	    while ((c = is.read()) != -1) {
		out.writeByte(c);
	    }

	    out.close();
	} catch (IOException e) {
	    System.err.println("Error Writing/Reading Streams.");
	}
    }

}
